// pages/dl/dl.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'',//手机号
    txts: '获取验证码',
    yzmstatus: 1,
    disab: false,
    times: 1,
    verifycode:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {

  },
  sj:function(e){
    var phone = e.detail.value
    this.setData({
      phone: phone
    })
  },
  yzm:function(e){
    var verifycode = e.detail.value
    this.setData({
      verifycode: verifycode
    })
  },
  yzmsend: function (e) {
    var that = this
    //获取网络状态 
    wx.getNetworkType({
      success: function (res) {
        console.log('load网络连接状态', res.networkType)
        if (res.networkType != 'none') {
          if (that.data.phone.length == 11) {
            app.util.request({
              url: "entry/wxapp/Api",
              data: {
                m: "monai_market",
                uid: wx.getStorageSync("uid"),
                r: "extends.getMsgcode",
                phone: that.data.phone,
              }, success: function (e) {
                if (e.data.errno == 0) {
                console.log('chenggong',e)
                wx.showToast({
                  title: '发送成功',
                  icon: "none",
                  duration: 2000
                })
                that.sshow();
              } else {
                wx.showToast({
                  title: e.message,
                  duration: 2000
                })
              }
            }
            });
          } else {
            wx.showToast({
              title: '请输入正确的手机号',
              icon: "none",
              duration: 2000
            })
          }
        } else {
          wx.showToast({
            title: '无网络连接',
            icon: "none",
            duration: 2000
          })
        }
      }
    })
  },
  sshow: function () {
    this.setData({
      disab: true,
      txts: "60秒",
      yzmstatus: 2,
    })
    var timer = this.data.times
    if (timer == 1) {
      timer = 0
      var that = this
      var time = 59
      var inter = setInterval(function () {//循环执行代码  setInterval
        that.setData({
          txts: time + "秒"
        })
        time--
        if (time < 0) {
          timer = 1
          clearInterval(inter)
          that.setData({
            disab: false,
            txts: "发送验证码",
            yzmstatus: 1,
          })
        }
      }, 1000)
    }
  },
  dl:function(e){
    var that = this;
    if (that.data.phone.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: "none",
        duration: 1000
      })
      return;
    } else if (that.data.phone != that.data.wxphone && (that.data.verifycode.length > 6 || that.data.verifycode.length < 4)) {
      wx.showToast({
        title: '手机验证码格式错误',
        icon: "none",
        duration: 1000
      })
    }
    app.util.request({
      url: "entry/wxapp/Api",
      data: {
        m: "monai_market",
        uid: wx.getStorageSync("uid"),
        r: "extends.verifycode",
        phone: that.data.phone,
        code: that.data.verifycode,
      }, success: function (e) {
        if(e.data.errno==0){
          app.globalData.phone = that.data.phone
          wx.navigateTo({
            url: '../enter/enter'
          })
        }else{
          wx.showToast({
            title: '绑定失败',
            icon: "none",
            duration: 1000
          })
        }
        console.log('成功',e)
      }
    })
  }
})